﻿using System;

using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using Silvermoon.Controls.Interfaces;
using System.Collections.ObjectModel;
using Silvermoon.Controls.Base;

namespace Silvermoon.Controls.Classes
{

    public abstract class TypedControlCollection<O, T> : Collection<T>
        where T : Control
        where O : Control
    {
        private INotifyControlCollection notifyOwner;
        private O owner;
        public O Parent { get { return owner; } }

        //public T[] Controls { get { return  this. } }

        public TypedControlCollection(O owner)
            : base()
        {
            this.owner = owner;
            this.notifyOwner = owner as INotifyControlCollection;
        }

        protected virtual void OnAddControl(T c)
        {
            if (notifyOwner != null) notifyOwner.ControlAdded(c);
        }

        protected virtual void OnControlRemoved(T c)
        {
            if (notifyOwner != null)
            {
                notifyOwner.ControlRemoved(c);
            }
        }

        protected virtual void OnClear()
        {
            if (notifyOwner != null) notifyOwner.CollectionCleared();
        }

        protected override void RemoveItem(int index)
        {
            T control = this[index];
        //    control.Parent = null;
            base.RemoveItem(index);
            OnControlRemoved(control);
        }

        protected override void InsertItem(int index, T item)
        {
        //    item.Parent = owner;
            base.InsertItem(index, item);
            OnAddControl(item);
        }

        protected override void ClearItems()
        {
            OnClear();
            base.ClearItems();
        }

    }

}
